//
//  Util.m
//  DarkSunVideo
//
//  Created by WANG TOM on 11-4-25.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "Util.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SLFoundation.h"

NSString * doDESCipher(CCAlgorithm desOr3des, CCOperation encryptOrDecrypt, NSString *plainText, NSString *key){
    
    
    const void *vplainText;
    
    size_t plainTextBufferSize;
    
    
    if(encryptOrDecrypt == kCCDecrypt){
        
        
        NSData *EncryptData = [[NSData alloc] initWithBase64EncodedString:plainText];
        
        plainTextBufferSize = [EncryptData length];
        
        vplainText = [EncryptData bytes];
        
        [EncryptData release];
        
        
    }else{
        
        
        plainTextBufferSize = [plainText length];
        
        vplainText = (const void *) [plainText UTF8String];
        
    }
    
    
    uint8_t *bufferPtr = NULL;
    
    size_t bufferPtrSize = 0;
    
    size_t movedBytes = 0;
    
    
    if(desOr3des == kCCAlgorithm3DES){
        
        bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
        
    }else if(desOr3des == kCCAlgorithmDES){
        
        bufferPtrSize = (plainTextBufferSize + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
        
    }
    
    
    bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
    
    memset((void *)bufferPtr, 0x0, bufferPtrSize);
    
    
   // NSString *initVec = [key copy];
    
    const void *vkey = (const void *) [key UTF8String];
    
  //  const void *vinitVec = (const void *) [initVec UTF8String];
    
    
    CCCryptorStatus ccStatus = CCCrypt(encryptOrDecrypt,
                                       
                                       desOr3des,
                                       
                                       kCCOptionPKCS7Padding|kCCOptionECBMode,
                                       
                                       vkey,
                                       
                                       kCCKeySizeDES,
                                       
                                       nil,
                                       
                                       vplainText,
                                       
                                       plainTextBufferSize,
                                       
                                       (void *)bufferPtr,
                                       
                                       bufferPtrSize,
                                       
                                       &movedBytes);
    
    
    if(ccStatus == kCCSuccess){
        
        NSString *result = nil;
        
        if (encryptOrDecrypt == kCCDecrypt){
            
            result = [[[NSString alloc] initWithData:
                       
                       [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes] 
                       
                                            encoding:NSASCIIStringEncoding] autorelease];
            
        }else{
            
            NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
            
            result = [myData base64EncodingWithLineLength:0];
            
        }
        
        return result;
        
    }else{
        
        NSLog(@"*** ERROR: doDESCipher, code: %d",ccStatus);
        
    }
    
    
    return nil;
    
    
}




NSString * httpGet(NSString *url)
{
    NSURL *nsUrl = [NSURL URLWithString:url];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:nsUrl];
    [request startSynchronous];
    NSError *error = [request error];
    if (!error) {
        NSString *response = [request responseString];
        return response;
    }else{
        return nil;
    }
    
}
void httpGetAsy(NSString *url,id delegate)
{
    NSURL *nsUrl = [NSURL URLWithString:url];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:nsUrl];
    [request setDelegate:delegate];
    [request startAsynchronous];
}

NSString * httpPost(NSString *url,NSDictionary *valueMap)
{
    NSURL *nsUrl = [NSURL URLWithString:url];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:nsUrl];
    for(NSString *name in valueMap)
    {
         [request setPostValue: [valueMap objectForKey:name] forKey:name];
    }
 
    [request startSynchronous];
    NSError *error = [request error];
    if (!error) {
        NSString *response = [request responseString];
        return response;
    }else{
        return nil;
    }
}
void httpPostAsy(NSString *url,NSDictionary *valueMap,id delegate)
{
    NSURL *nsUrl = [NSURL URLWithString:url];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:nsUrl];
    for(NSString *name in valueMap)
    {
        [request setPostValue: [valueMap objectForKey:name] forKey:name];
    }
     [request setDelegate:delegate];
    [request startAsynchronous];
        
}
BOOL wget(NSString *url,NSString *desitionPath)
{
  
    NSURL *nsUrl = [NSURL URLWithString:url];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:nsUrl];
    [request setDownloadDestinationPath:desitionPath];
    
    [request startSynchronous];
    NSError *error = [request error];
    if (!error) {
        //NSString *response = [request responseString];
        return YES;
    }else{
        return NO;
    }
}


Boolean isValidEmailAddress(NSString *istr)
{
   NSString* str = trim(istr);
    
    NSString *regex = @"[a-zA-Z0-9\\.\\-\\_\\+]+\\@[a-zA-Z0-9\\.\\-\\_\\+]+\\.[a-zA-Z0-9\\-]{2,3}";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    if(![pred evaluateWithObject:str]){
        return NO;
    }
    return YES;
}
NSString * md5(NSString *str)
{
    const char *cStr = [str UTF8String]; 
    unsigned char result[32]; 
    CC_MD5( cStr, strlen(cStr), result ); 
    return [NSString stringWithFormat: 
            @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            result[0], result[1], result[2], result[3], 
            result[4], result[5], result[6], result[7], 
            result[8], result[9], result[10], result[11], 
            result[12], result[13], result[14], result[15] 
            ]; 
}
